package muhnes.sizoo.dal;

import java.util.List;
import javax.annotation.PreDestroy;
import muhnes.sizoo.HibernateUtilZoologia;
import muhnes.sizoo.modelo.Zona;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ZonaDaoImpl implements ZonaDao {
    
    Session sesion;
    
    public ZonaDaoImpl() {
        sesion = HibernateUtilZoologia.getSessionFactory().openSession();
    }
    
    @PreDestroy
    public void destroy() {
        sesion.close();
    }

    @Override
    public void guardar( Zona zona ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.save( zona );
        transaccion.commit();
    }

    @Override
    public void guardarColeccion( List< Zona > zonas ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.saveOrUpdate( zonas );
        transaccion.commit();
    }

    @Override
    public void actualizar( Zona zona ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( zona );
        transaccion.commit();
    }

    @Override
    public void actualizarColeccion( List< Zona > zonas ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( zonas );
        transaccion.commit();
    }

    @Override
    public void eliminar( Zona zona ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( zona );
        transaccion.commit();
    }

    @Override
    public void eliminarColeccion( List< Zona > zonas ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( zonas );
        transaccion.commit();
    }

    @Override
    public void eliminarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Zona zona = buscarPorId( id );
        sesion.delete( zona );
        transaccion.commit();
    }

    @Override
    public Zona buscarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Zona where codigozona = " + id);
        Zona zona = (Zona)consulta.uniqueResult();
        transaccion.commit();
        return zona;
    }

    @Override
    public List< Zona > buscarTodos() {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Zona");
        List< Zona > zonas = consulta.list();
        transaccion.commit();
        return zonas;
    }

}
